#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <poll.h>
#include <linux/input.h>

#include "vser.h"

int main(int argc, char* argv[])
{
    int ret;
    struct pollfd fds[2];
    char rbuf[32];
    char wbuf[32];
    struct input_event key;

    // 打开第一个设备
    fds[0].fd = open("/dev/vser0", O_RDWR | O_NONBLOCK);
    if (fds[0].fd == -1) {
        goto fail;
    }

    fds[0].events = POLLIN;
    fds[0].revents = 0;

    // 打开第二个设备
    fds[1].fd = open("/dev/event1", O_RDWR | O_NONBLOCK);
    if (fds[1].fd == -1) {
        goto fail;
    }

    fds[1].events = POLLIN;
    fds[1].revents = 0;

    // 循环处理事件
    while (1) {
        ret = poll(fds, 2, -1);
        if (ret == -1) {
            goto fail;
        }

        if (fds[0].revents & POLLIN) {
            ret = read(fds[0].fd, rbuf, sizeof(rbuf));
            if (ret < 0) {
                goto fail;
            }
            puts(rbuf);
        }

        if (fds[1].revents & POLLIN) {
            ret = read(fds[1].fd, &key, sizeof(key));
            if (ret < 0) {
                goto fail;
            }

            if (key.type == EV_KEY) {
                sprintf(wbuf, "%#x\n", key.code);
                ret = write(fds[0].fd, wbuf, strlen(wbuf) + 1);
                if (ret < 0) {
                    goto fail;
                }
            }

        }
    }

fail:
    perror("poll test");
    exit(EXIT_FAILURE);
}